
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>待办事项清单</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        .task-completed {
            text-decoration: line-through;
            opacity: 0.7;
        }
        .fade-in {
            animation: fadeIn 0.3s ease-in;
        }
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(10px); }
            to { opacity: 1; transform: translateY(0); }
        }
    </style>
</head>
<body class="bg-gray-100 min-h-screen">
    <div class="container mx-auto max-w-3xl px-4 py-8">
        <div class="bg-white rounded-xl shadow-lg overflow-hidden">
            <div class="bg-gradient-to-r from-blue-500 to-purple-600 p-6 text-white">
                <h1 class="text-3xl font-bold">待办事项清单</h1>
                <p class="mt-2 opacity-90">记录你的每日任务</p>
            </div>

            <div class="p-6">
                <div class="flex mb-6">
                    <input 
                        type="text" 
                        id="taskInput" 
                        placeholder="添加新任务..." 
                        class="flex-grow px-4 py-2 border border-gray-300 rounded-l-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
                    >
                    <button 
                        id="addBtn" 
                        class="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded-r-lg transition duration-200"
                    >
                        <i class="fas fa-plus mr-1"></i> 添加
                    </button>
                </div>

                <div class="flex justify-between mb-4">
                    <div class="text-gray-600">
                        <span id="taskCount">0</span> 个任务
                    </div>
                    <div>
                        <button id="clearAllBtn" class="text-red-500 hover:text-red-700">
                            <i class="fas fa-trash-alt mr-1"></i> 清空所有
                        </button>
                    </div>
                </div>

                <div id="taskList" class="space-y-2">
                    <!-- 任务将在这里动态生成 -->
                </div>
            </div>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const taskInput = document.getElementById('taskInput');
            const addBtn = document.getElementById('addBtn');
            const taskList = document.getElementById('taskList');
            const taskCount = document.getElementById('taskCount');
            const clearAllBtn = document.getElementById('clearAllBtn');
            
            let tasks = JSON.parse(localStorage.getItem('tasks')) || [];
            
            function renderTasks() {
                taskList.innerHTML = '';
                tasks.forEach((task, index) => {
                    const taskElement = document.createElement('div');
                    taskElement.className = `bg-white border border-gray-200 rounded-lg p-3 flex items-center justify-between fade-in ${task.completed ? 'task-completed' : ''}`;
                    taskElement.innerHTML = `
                        <div class="flex items-center">
                            <input 
                                type="checkbox" 
                                ${task.completed ? 'checked' : ''} 
                                class="mr-3 h-5 w-5 rounded border-gray-300 text-blue-600 focus:ring-blue-500 cursor-pointer"
                                data-index="${index}"
                            >
                            <span>${task.text}</span>
                        </div>
                        <button 
                            class="text-red-500 hover:text-red-700 delete-btn" 
                            data-index="${index}"
                        >
                            <i class="fas fa-times"></i>
                        </button>
                    `;
                    taskList.appendChild(taskElement);
                });
                
                taskCount.textContent = tasks.length;
                updateLocalStorage();
            }
            
            function addTask() {
                const text = taskInput.value.trim();
                if (text) {
                    tasks.push({ text, completed: false });
                    taskInput.value = '';
                    renderTasks();
                }
            }
            
            function deleteTask(index) {
                tasks.splice(index, 1);
                renderTasks();
            }
            
            function toggleComplete(index) {
                tasks[index].completed = !tasks[index].completed;
                renderTasks();
            }
            
            function clearAllTasks() {
                if (confirm('确定要删除所有任务吗？')) {
                    tasks = [];
                    renderTasks();
                }
            }
            
            function updateLocalStorage() {
                localStorage.setItem('tasks', JSON.stringify(tasks));
            }
            
            addBtn.addEventListener('click', addTask);
            taskInput.addEventListener('keypress', function(e) {
                if (e.key === 'Enter') addTask();
            });
            
            clearAllBtn.addEventListener('click', clearAllTasks);
            
            taskList.addEventListener('click', function(e) {
                if (e.target.classList.contains('delete-btn') || e.target.parentElement.classList.contains('delete-btn')) {
                    const index = e.target.dataset.index || e.target.parentElement.dataset.index;
                    deleteTask(index);
                } else if (e.target.type === 'checkbox') {
                    toggleComplete(e.target.dataset.index);
                }
            });
            
            renderTasks();
        });
    </script>
</body>
</html>
